package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.VersionInfoRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.PstAssign;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.Menu;
import com.ndp.fb.rdb.model.version.VersionInfo;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by elvisxiong on 16/2/26.
 */
@Service
public class VersionInfoRdbServiceImpl extends DaoTemplate implements VersionInfoRdbService {
    @Override
    public Long save(VersionInfo versionInfo) {
        String sql = "INSERT INTO version_info (" +
                "version,body,user_id,dictionary_id,update_time" +
                ") VALUES (" +
                "?,?,?,?,?"+
                ")";
        PstAssign psta = new PstAssign() {
            @Override
            public void setParam(PreparedStatement ps) throws SQLException {
                ps.setString(1, versionInfo.getVersion());
                ps.setString(2, versionInfo.getBody());
                ps.setLong(3,versionInfo.getUserId());
                ps.setLong(4,versionInfo.getDictionaryId());
                ps.setLong(5,versionInfo.getUpdateTime());
            }
        };
        return saveResultId(sql, psta);
    }

    @Override
    public List<VersionInfo> getVersionInfoList(Long dictionaryId) {
        String sql = "select * from version_info where dictionary_id = ? order by create_time desc";
        return getJdbcTemplate().query(sql, new Object[]{dictionaryId},DefaultRowMapper.autoRowMapper(VersionInfo.class));
    }

    @Override
    public void delete(Long id) {
        String sql = "delete from version_info where id = ?";
        getJdbcTemplate().update(sql, new Object[]{id});
    }

    @Override
    public String getCurrentVersionInfo() {
        String sql = "select version from version_info order by version DESC limit 0,1";
        return getJdbcTemplate().queryForObject(sql,String.class);
    }

}
